package com.hy.dao;

import com.hy.bean.TableLike;
import com.hy.utils.DBManager;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @Description:
 * @Author: zhangzhengwei2006
 * @Date: 2024-01-17 9:02
 *
 *
 */
public class CommentDao {
    private QueryRunner queryRunner = new QueryRunner();

    // 逻辑删除帖子下的所有评论
    public int deleteByCardId(Integer id) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "update comment set tombstone = '1' where card_id = ?";
        int update = queryRunner.update(conn, sql, id);
        DbUtils.close(conn);
        return update;
    }

    public List<Map<String, Object>> selectBy(Integer cid, Integer pid) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "select c.id,comment_content,user_name,img,c.create_time,number from `comment` c,ums_user u where c.user_id = u.id and c.card_id = ? and c.parent_id = ? order by c.number desc";
        List<Map<String, Object>> mapList = queryRunner.query(conn, sql, new MapListHandler(), cid, pid);
        DbUtils.close(conn);
        return mapList;
    }

    public void select(List<Map<String, Object>> mapList , Integer id) throws Exception {
        CommentDao c = new CommentDao();
        for(Map<String,Object> map : mapList){
            List<Map<String, Object>> maps = c.selectBy( id,(Integer) map.get("id"));
            for(Map<String, Object> mapp : maps){
                mapp.put("create_time", mapp.get("create_time").toString());
            }
            map.put("children",maps);
            c.select((List<Map<String, Object>>) map.get("children"),id);
        }
    }


    // 逻辑删除
    public int deleteById(Integer id,String state) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "update comment set tombstone = ? where id = ?";
        int update = queryRunner.update(conn, sql, state, id);
        DbUtils.close(conn);
        return update;
    }

//    public static void main(String[] args) throws Exception {
//        List<Map<String, Object>> mapList = CommentDao.selectBy(1, 0);
//        CommentDao.select(mapList,1);
//
//        System.out.println(mapList);
//    }
    public boolean updateById(Integer id) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "update comment set number = number+1 where id=?";
        int count = queryRunner.update(conn, sql, id);
        DbUtils.close(conn);
        return count > 0 ? true:false;
    }

    public boolean updateById2(Integer id) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "update comment set number = number-1 where id=?";
        int count = queryRunner.update(conn, sql, id);
        DbUtils.close(conn);
        return count > 0 ? true:false;
    }

    public boolean select(Integer uid,Integer cid) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "select count(*) from table_like where uid=? and cid=? and tombstone='0'";
        Long count = queryRunner.query(conn, sql,new ScalarHandler<Long>(), uid,cid);
        DbUtils.close(conn);
        return count > 0 ? true:false;
    }

    public boolean insert(TableLike tableLike) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "insert into table_like values (?,?,?,?,?,?,?,?)";
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        int count = queryRunner.update(conn, sql, tableLike.getUid(), tableLike.getCid(), tableLike.getStatus(), tableLike.getCreateId(), tableLike.getUpdateId(), formatter.format(date), tableLike.getUpdateTime(), tableLike.getTombstone());
        DbUtils.close(conn);
        return count > 0 ? true:false;
    }

    public boolean updateByTom(TableLike tableLike) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "update table_like set tombstone=?,update_time=?,update_id=? where  uid=? and cid=? ";
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        int count = queryRunner.update(conn, sql,tableLike.getTombstone(), formatter.format(date),tableLike.getUpdateId(),tableLike.getUid(),tableLike.getCid());
        DbUtils.close(conn);
        return count > 0 ? true:false;
    }

    public static void main(String[] args) throws Exception {
        CommentDao dc = new CommentDao();
        System.out.println(dc.updateByTom(new TableLike(1,4,"1", null,1,null,null, "1")));
    }
}
